<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Administrator_award extends MY_Administrator {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/home
	 *	- or -  
	 * 		http://example.com/index.php/home/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
    function __construct()
    {
        parent::__construct();
		//Load Database
		$this->DB_AWARD = $this->load->database("you2play_award_2014",TRUE);
		//Config Initail
		$this->artist_folder = $this->upload_folder.'award/artist2014/';
		//Load Model
		$this->load->model('administrator_award_model');
    }
	
	/*Monitor*/
	public function monitor($module=NULL,$arg=NULL)
	{
		switch($module):
			case 'update':
				$this->administrator_award_model->monitor_update();
				redirect($this->session->userdata('returnUrl'));			
			break;
			case 'ajax_load_score':
				$this->administrator_award_model->ajax_load_score();
			break;							
			default:
				/*Keep Url*/
				$this->session->set_userdata('returnUrl', base_url($this->input->server('REQUEST_URI')));
				
				$this->data['dataResult'] = $this->DB_AWARD->get('award_vote_score',1)->row();
				//Load XML
				for($i=1;$i<=12;$i++):
					for($j=1;$j<=6;$j++):
						$this->data['dataXML'.sprintf('%02d',$i).$j] = simplexml_load_file($this->artist_folder.sprintf('%02d',$i)."/".sprintf('%02d',$j)."/data.xml");
					endfor;
				endfor;				

				$this->data['content'] = 'administrator/award/'.$this->uri->rsegment(2);
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);			
		endswitch;
	}
	
	/*Comment*/
	public function comment($module=NULL,$arg=NULL)
	{
		switch($module):
			case 'refresh':
				$this->administrator_award_model->comment_refresh();
				redirect($this->session->userdata('returnUrl'));			
			break;	
			case 'update':
				$this->administrator_award_model->comment_update();
				redirect($this->session->userdata('returnUrl'));			
			break;									
			default:
				/*Keep Url*/
				$this->session->set_userdata('returnUrl', base_url($this->input->server('REQUEST_URI')));
				
				/*Search Condition*/
				$searchCondition = array("comment_id IS NOT NULL");
				$cat_id = $this->input->get('cat_id');
				if(!empty($cat_id)) $searchCondition[] = "cat_id = ".$cat_id."";			
				
				$this->data['dataResult'] = $this->DB_AWARD->order_by('likes','DESC')->get_where('award_comment',''.implode(' AND ',$searchCondition).'')->result();
				/*Find Total Active*/
				$searchCondition[] = "is_active='1'";
				$this->data['totalActive'] = $this->DB_AWARD->from('award_comment')->where(''.implode(' AND ',$searchCondition).'')->count_all_results();

				$this->data['content'] = 'administrator/award/'.$this->uri->rsegment(2);
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);			
		endswitch;
	}
}